<?php

class ReportsController extends AppController {

    var $name = 'Reports';
//    var $components = array('RequestHandler', 'Access');
    var $uses = array('Report');
    var $components = array('RequestHandler', 'Access');
    
    function beforeFilter() {
        parent::beforeFilter();
        // echo $this->Session->setFlash("Welcome user");
        $this->Auth->autoRedirect = false;
        $this->Auth->allow('report', 'test');
        $this->Auth->loginRedirect = array('controller' => 'Homes', 'action' => 'index');
    }
    
    function test(){
//        Configure::write('debug', 0);
//        debug($stats);die();
    
        $tz_string = "UTC"; // Use one from list of TZ names http://php.net/manual/en/timezones.php 
        $tz_object = new DateTimeZone($tz_string);    
        $datetime = new DateTime(); 
        $datetime->setTimezone($tz_object);
        
        $user_id = 1;//$_POST['rtype_id'];
        $type_id = 1;//$_POST['rtype_id'];
        $target_id = 1;//$_POST['rtarget_id'];
        $content = 'some content';//$_POST['rcontent'];
        $time = $datetime->format('Y-m-d H:i:s');
        
        $report = array('type_id'=>$type_id, 'target_id'=>$target_id, 'user_id'=>$user_id, 'content'=>$content, 'time'=>$time);
        $r = $this->Report->save($report);var_dump($r);die();
    }
    
    function report(){
        $uri = $_SERVER[ 'REQUEST_URI' ];
        $uri_arr = explode("/",$uri);
        
        if(!$this->RequestHandler->isAjax())
            $this->redirect(array('controller' => 'homes', 'action' => 'index'));

        if (isset($_SESSION['Auth']['user']['User']['id']))
            $user_id = $_SESSION['Auth']['user']['User']['id'];
        else
            die();
        
        $tz_string = "UTC"; // Use one from list of TZ names http://php.net/manual/en/timezones.php 
        $tz_object = new DateTimeZone($tz_string);    
        $datetime = new DateTime(); 
        $datetime->setTimezone($tz_object);
//        echo $datetime->format('Y-m-d H:i:s'); // Prints "2011-03-20 07:16:17"
        
        $this->set('title_for_layout', 'Rate The Universe - Report');
        
        $type_id = $_POST['rtype_id'];
        $target_id = $_POST['rtarget_id'];
        $content = $_POST['rcontent'];
        $ref_id = $_POST['rref_id'];
        $time = $datetime->format('Y-m-d H:i:s');
        
        $report = array('ref_id'=> $ref_id,'type_id'=>$type_id, 'target_id'=>$target_id, 'user_id'=>$user_id, 'content'=>$content, 'time'=>$time);
        $rows_affected = $this->Report->save($report);
        
        $result = array('result'=>false,'id'=> 0);
        if(count($rows_affected['Report'])>0){
            $result['result'] = true;
            $result['id'] = $this->Report->id;
        }
        echo json_encode($result);die();
    }
}

?>